﻿// -----------------------------------------------------------------------
// <copyright file="TesDelivery.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace DosTexPrj.Implement.NHibernate.Test.Repositories
{
    using System;
    using System.Linq;
    using DosTexPrj.Core.Models;
    using DosTexPrj.Core.RepositoriesInterface;
    using NUnit.Framework;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    [TestFixture]
    public class TesDelivery : RepositoryFixtureBase
    {
        private IRepository _repository;
        private DeliveryMethod _deliveryMethod;
        private PaymentMethod _paymentMethod;
        private Composition _composition;
        private OrderStatus _orderStatus;
        private FabricType _fabricType;

        protected override void Context()
        {
            this._repository = MyServiceLocator.DoGetInstance<IRepository>();
            this._deliveryMethod = this._repository.FindByKey<DeliveryMethod>(1);
            this._paymentMethod = this._repository.FindByKey<PaymentMethod>(1);
            this._composition = this._repository.FindByKey<Composition>(1);
            this._orderStatus = this._repository.FindByKey<OrderStatus>(1);
            this._fabricType = this._repository.FindByKey<FabricType>(1);
        }

        [Test]
        public void Can_Create_Delivery()
        {
            this.SaveDelivery();
        }

        #region --- Private Methods ---

        private Order SaveOrder()
        {
            var customer = this.CreateDummyCustomer("TestDelivery");
            customer = this._repository.Save<Customer>(customer);
            var order = this.CreateDummyOrder(customer, _paymentMethod, _deliveryMethod, _orderStatus);
            var orderDetails = this.CreateDummyOrderDetails(order, _composition, _fabricType);
            order.OrderDetails = orderDetails;
            order = this._repository.Save<Order>(order);
            return order;
        }

        private void SaveDelivery()
        {
            var order = this.SaveOrder();
            var orderDetailToDelivery = order.OrderDetails[0];
            var delivery = new Delivery();
            delivery.BelongToOrder = order;
            delivery.DeliveryAddress = "temp";
            delivery.DeliveryDate = DateTime.Today;
            delivery.Details.Add(new DeliveryDetail
            {
                BelongToDelivery = delivery,
                KgQuantity = 500,
                MtQuantity = 500,
                ReferenceToOrderDetail = order.OrderDetails[0]
            });
            delivery.Details.Add(new DeliveryDetail
            {
                BelongToDelivery = delivery,
                KgQuantity = 500,
                MtQuantity = 500,
                ReferenceToOrderDetail = orderDetailToDelivery
            });
            delivery = this._repository.Save<Delivery>(delivery);
            Assert.IsTrue(delivery != null && delivery.Id > 0
                            && delivery.Details != null && delivery.Details.Count == 2);

            var deliveriesOfOrder = this._repository.FindByCondition<DeliveryDetail>(x => x.ReferenceToOrderDetail == orderDetailToDelivery);
            double totalDeliveried = deliveriesOfOrder.Sum(x => x.KgQuantity);
            if (totalDeliveried == orderDetailToDelivery.KgQuantity)
            {
                orderDetailToDelivery.IsDone = true;
                orderDetailToDelivery = this._repository.Save<OrderDetail>(orderDetailToDelivery);
            }
            Assert.IsTrue(orderDetailToDelivery.KgQuantity == totalDeliveried && orderDetailToDelivery.IsDone == true);
        }

        #endregion --- Private Methods ---
    }
}